package com.youxue.teachin.user.resetpassword;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.Navigator;
import com.library.rest.ApiServiceManager;
import com.library.rest.rxjava.RxResultHelper;
import com.library.rest.rxjava.RxSchedulersHelper;
import com.library.uiframework.activity.ActionBarLayoutActivity;
import com.library.uiframework.utils.MToast;
import com.library.uiframework.utils.PatternUtils;
import com.library.uiframework.utils.StringUtils;
import com.library.uiframework.widget.actionbar.ActionBarLayout.Action;
import com.youxue.teachin.BuildConfig;
import com.youxue.teachin.R;
import com.youxue.teachin.common.utils.ActivityCacheHelper;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.user.repository.IUserRepository;

import static com.library.uiframework.slidingclose.SlidingClosableRelativeLayout.SLIDING_CLOSE_MODE_NONE;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-05 17:14
 * @version: 9.1.0
 */
public class ResetPasswordActivity extends ActionBarLayoutActivity  implements OnClickListener{

    private EditText mLoginInputPhone;
    private EditText mLoginInputCaptcha;
    private TextView mGetCaptcha;
    private TextView mRegistBt;

    @Override
    protected void onActionClick(int actionId, Action action) {
        super.onActionClick(actionId, action);

    }

    @Override
    protected void onInitActionBar() {
        super.onInitActionBar();
        setTitle("密码重置");
    }

    @Override
    protected Action createBack() {
        return addLeftImageAction(ACTION_BACK, R.drawable.back);
    }

    @Override
    protected boolean needMenuAction() {
        return false;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        initView();
        getActionBarLayout().setFloatingModeAlpha(0);
        getActionBarLayout().getTitleView().setAlpha(1);
        setTitleColor(getResources().getColor(R.color.cl_1));
        setSlidingCloseMode(SLIDING_CLOSE_MODE_NONE);
    }

    @Override
    protected View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.user_reset_password_activity, container, false);
        return view;
    }

    private void initView() {
        mLoginInputPhone = (EditText)findViewById(R.id.login_input_phone);
        mLoginInputCaptcha = (EditText)findViewById(R.id.login_input_captcha);
        mGetCaptcha = (TextView)findViewById(R.id.get_captcha);
        mRegistBt = (TextView)findViewById(R.id.regist_bt);
        mRegistBt.setOnClickListener(this);
        mGetCaptcha.setOnClickListener(this);

        if (BuildConfig.DEBUG) {
            mLoginInputPhone.setText("18501714348");
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.get_captcha) {
            getcaptcha();
        } else if (id == R.id.regist_bt) {
            next();
        }
    }

    private void next() {
        String phone = mLoginInputPhone.getText().toString();
        if (!PatternUtils.isMobile(phone)) {
            MToast.getInstance().setText("手机号码输入不正确");
            return;
        }

        String captcha = mLoginInputCaptcha.getText().toString();
        if (StringUtils.isEmpty(captcha)) {
            MToast.getInstance().setText("请输入验证码");
            return;
        }
        ActivityCacheHelper.registActivity(this);
        new Navigator.Builder()
            .setPageName("reset_password")
            .addParameter("phone", phone)
            .addParameter("captcha", captcha)
            .addParameter("type", 1)
        .build().open();
    }

    private void getcaptcha() {
        String phone = mLoginInputPhone.getText().toString();
        if (!PatternUtils.isMobile(phone)) {
            MToast.getInstance().setText("手机号码输入不正确");
            return;
        }

        Class<IUserRepository> clazz = IUserRepository.class;
        ApiServiceManager.getOrCreateApiService(clazz)
            .getCaptcha(phone, IUserRepository.CAPTCHA_UPD)
            .compose(RxResultHelper.<String>handleResult())
            .compose(RxSchedulersHelper.<String>io_main())
            .subscribe(new DefaultSubscriber<String>(){
                @Override
                public void onNext(String s) {
                    super.onNext(s);
                    MToast.getInstance().setText("验证码发送成功");
                }
            });
    }
}
